/***************************************************************
*  Copyright (c) 2007 by GroupMe! Team (www.groupme.net)
*  All rights reserved.
*
*  This file is part of the GroupMe! Project. Source code of 
*  this project is closed and redistribution of this code is
*  prohibited. 
*  
*  Contact: http://www.groupme.net
*
*  This copyright notice MUST APPEAR in all copies of the file!
***************************************************************/
package net.groupme.utils.sql;

import java.sql.Types;
import java.util.Collection;

import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.object.SqlUpdate;
import org.springframework.jdbc.support.GeneratedKeyHolder;

import net.groupme.exceptions.InsertFailedException;
import net.groupme.model.Group;
import net.groupme.model.MODEL_CONSTANTS;
import net.groupme.model.ObjectFactory;
import net.groupme.model.Resource;
import net.groupme.model.Tag;
import net.groupme.model.dao.DATABASE_CONSTANTS;

/**
 * created on Sep 24, 2007
 * 
 * Sone administrative queries like listing groups of users...
 * 
 * @author Fabian Abel, <a href="mailto:abel@l3s.de">abel@l3s.de</a>
 * @author last edited by: $Author: fabian $
 * 
 * @version $Revision: 1.3 $ $Date: 2008-11-07 16:24:06 $
 */
public class AdminUtilityQuerying {
	
	/**
	 * Execute the stuff below...
	 */
	public static void main(String[] args) {
		String[] tags = {
		 "play online chess",
		 "fencing fight touche!",
		 "young fencers funny",
		 "japanise traditional fencing budo",
		 "fencing online resource",
		 "basic information chess wikipedia",
		 "online hints",
		 "chess board pic",
		 "game over chess board",
		 "cocktail martini glass ice",
		 "martini glass ice",
		 "shaker lime cocktail",
		 "vodka absolut bottles",
		 "wine glass bottle",
		 "becks beer kiss bottle girl love",
		 "bar alcohol bottles happiness",
		 "vodka best nemiroff ukranian pepper honey",
		 "best vodka brand nemiroff"
		};
		for (int i = 0; i < tags.length; i++) {
			separateTags(tags[i]);
		}
	}

	/**
	 * Separiting given keywords into single tags...
	 * @param keyword e.g. "Semantic Web" will be splitted into "semantic" and "web"
	 */
	public static void separateTags(String keyword/*, String userId, String date, String group, String resource*/){
		Tag oldTag = ObjectFactory.getTag(keyword);
		Collection<Resource> taggedResources = oldTag.getResources();
		
		String[] keywords = keyword.split(" ");
		for (int i = 0; i < keywords.length; i++) {
			try {
				Tag tag = ObjectFactory.getTagForKeyword(keywords[i]);
				
				for (Resource resource: taggedResources) {
					Group group = resource.getGroups().iterator().next();
					String query = "INSERT INTO `userTagGroupResource` (`userId`, `tagId`, `groupId`, `resourceId`, `date`, `importance`) " +
					" VALUES ('28', '" + tag.getId() + "', " + (MODEL_CONSTANTS.RESOURCE_TYPE_GROUP.equals(resource.getType()) ? "NULL" : ("'" + group.getId() + "'")) + " , '" + resource.getId() + "', '2007-09-07', '1.0')";
				}
				
				/*SqlUpdate su = new SqlUpdate(ds, "INSERT INTO " + DATABASE_CONSTANTS.TABLE_USERTAGGROUPRESOURCE //$NON-NLS-1$
						+ " (userId, tagId, groupId, resourceId, date, importance) VALUES ('" + userId + "', '" + tag.getId() + "' , NULL, '271', '2007-09-07', '1.0') 
						+ " (" + DATABASE_CONSTANTS.TAG_KEYWORD + ", "
						+ DATABASE_CONSTANTS.TAG_DESCRIPTION + ") VALUES (?, ?)");
				su.declareParameter(new SqlParameter("keyword", Types.VARCHAR)); //$NON-NLS-1$
				su.declareParameter(new SqlParameter("description", Types.VARCHAR)); //$NON-NLS-1$
				su.compile();
				Object[] oa = new Object[2];
				oa[0] = new String(keyword);
				oa[1] = new String(description);
				GeneratedKeyHolder keyHolder = new GeneratedKeyHolder();
				int count = su.update(oa, keyHolder);*/
			} catch (InsertFailedException e) {
				e.printStackTrace();
			}
		}
		
		//identify tag assig
	}
}
